#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTimer>
#include <QDebug>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tw_ldList->setHeaderHidden(false);
    m_mqClient61850 = new MQClient61850;
    UpdateVal();

    m_timerPeriodRead = new QTimer;
    connect(m_timerPeriodRead, &QTimer::timeout, this, [this](){on_action_2_triggered();});
    connect(ui->tw_ldList, &QTreeWidget::itemDoubleClicked, this, [this](QTreeWidgetItem *item, int column){
        QTreeWidgetItem* traverseItem = item;
        QVector<quint8> t_locate;
        while (traverseItem->parent()!=nullptr) {
            t_locate.insert(0, traverseItem->parent()->indexOfChild(traverseItem));
            traverseItem = traverseItem->parent();
        }
        t_locate.insert(0, ui->tw_ldList->indexOfTopLevelItem(traverseItem));
        QString t_str2;
        if(t_locate.size()>3)
        {
            _61850_LN_ t_ln = m_mqClient61850->GetDirVec().at(t_locate.at(0)).lns.at(t_locate.at(1));

            if(t_ln.dos.size() > t_locate.at(2))
            {
                t_str2 = m_mqClient61850->GetDirVec().at(t_locate.at(0)).name + "/"
                        + m_mqClient61850->GetDirVec().at(t_locate.at(0)).lns.at(t_locate.at(1)).name + "."
                        + m_mqClient61850->GetDirVec().at(t_locate.at(0)).lns.at(t_locate.at(1)).dos.at(t_locate.at(2)).name + "."
                        + m_mqClient61850->GetDirVec().at(t_locate.at(0)).lns.at(t_locate.at(1)).dos.at(t_locate.at(2)).das.at(t_locate.at(3)).name;
                _61850_DA_ t_da = m_mqClient61850->GetDirVec().at(t_locate.at(0)).lns.at(t_locate.at(1)).dos.at(t_locate.at(2)).das.at(t_locate.at(3));
                for(int i=4; i<t_locate.size(); i++)
                {
                    t_da = t_da.structs.at(t_locate.at(i));
                    t_str2 += "." + t_da.name;
                }
            }
            else
            {
                _61850_FCDA_ t_fcda = t_ln.dataset.at(t_locate.at(2)-t_ln.dos.size()).fcdas.at(t_locate.at(3));
                t_str2 = t_fcda.name;
                for(int i=4; i<t_locate.size(); i++)
                {
                    t_fcda = t_fcda.structs.at(t_locate.at(i));
                    t_str2 += "." + t_fcda.name;
                }
            }
        }
        qDebug() << t_str2 << t_locate;
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::DealFcdaToTree(QTreeWidgetItem *t_treeWi, _61850_FCDA_ t_fcda)
{
    QTreeWidgetItem *t_twi_fcda = new QTreeWidgetItem(QStringList({t_fcda.name,
                                                                   (t_fcda.fc.isEmpty()?"":("["+t_fcda.fc+"]"))+t_fcda.type,
                                                                   t_fcda.value}));
    foreach(_61850_FCDA_ t_fcda_struct, t_fcda.structs)
    {
        DealFcdaToTree(t_twi_fcda, t_fcda_struct);
    }
    t_treeWi->addChild(t_twi_fcda);
}

void MainWindow::DealDaToTree(QTreeWidgetItem *t_treeWi, _61850_DA_ t_fcda)
{
    QTreeWidgetItem *t_twi_fcda = new QTreeWidgetItem(QStringList({t_fcda.name,
                                                                   (t_fcda.fc.isEmpty()?"":("["+t_fcda.fc+"]"))+t_fcda.type,
                                                                   t_fcda.value}));
    foreach(_61850_DA_ t_fcda_struct, t_fcda.structs)
    {
        DealDaToTree(t_twi_fcda, t_fcda_struct);
    }
    t_treeWi->addChild(t_twi_fcda);
}

void MainWindow::UpdateVal()
{
    ui->tw_ldList->clear();
    QVector<_61850_LD_> t_dirVec = m_mqClient61850->GetDirVec();
    foreach (_61850_LD_ t_ld, t_dirVec)
    {
        QTreeWidgetItem *t_twi_ld = new QTreeWidgetItem(QStringList(t_ld.name));
        foreach(_61850_LN_ t_ln, t_ld.lns)
        {
            QTreeWidgetItem *t_twi_ln = new QTreeWidgetItem(QStringList(t_ln.name));
            foreach(_61850_DO_ t_do, t_ln.dos)
            {
                QTreeWidgetItem *t_twi_do = new QTreeWidgetItem(QStringList({t_do.name, t_do.type}));
                foreach(_61850_DA_ t_da, t_do.das)
                {
                    DealDaToTree(t_twi_do, t_da);
                }
                t_twi_ln->addChild(t_twi_do);
            }
            foreach(_61850_DATASET_ t_dataset, t_ln.dataset)
            {
                QTreeWidgetItem *t_twi_dataset = new QTreeWidgetItem(QStringList({t_dataset.name,
                                                                                  t_dataset.type}));
                foreach(_61850_FCDA_ t_fcda, t_dataset.fcdas)
                {
                    DealFcdaToTree(t_twi_dataset, t_fcda);
                }
                t_twi_ln->addChild(t_twi_dataset);
            }
            t_twi_ld->addChild(t_twi_ln);
        }
        ui->tw_ldList->addTopLevelItem(t_twi_ld);
    }
//    ui->tw_ldList->setItemsExpandable(false);
    ui->tw_ldList->expandAll();
}


void MainWindow::on_action_2_triggered()
{
    m_mqClient61850->ClientUpdate();
    UpdateVal();
}

void MainWindow::on_action_3_triggered()
{
    ui->tw_ldList->clear();
}

void MainWindow::on_action_autoUpdate_toggled(bool arg1)
{
    if(arg1) m_timerPeriodRead->start(500);
    else m_timerPeriodRead->stop();
}

void MainWindow::on_actionwrite1_triggered()
{
//    m_mqClient61850->WriteVal("simpleIOGenericIO/GGIO1.Mod.t", MmsType::MMS_UTC_TIME, "ST", QVariant(QDateTime::currentMSecsSinceEpoch()));
//    m_mqClient61850->WriteVal("simpleIOGenericIO/GGIO1.Mod.stVal", MmsType::MMS_INTEGER, "ST", QVariant(3));
    m_mqClient61850->WriteVal("simpleIOGenericIO/GGIO1.NamPlt.vendor", MmsType::MMS_VISIBLE_STRING, "DC", QVariant("ddd"));
}


void MainWindow::on_action_triggered()
{
    ui->tw_ldList->collapseAll();
}

void MainWindow::on_action_expand_triggered()
{
    ui->tw_ldList->expandAll();
}
